window.vm = new Vue({
    el: '#app',

    data: {

        isClicked: false,
        imageUrl: '',
        question:'',
        user:'',
        name:'',
        process: {
            subject: '',
            content: '',
            proposer: '',
            questionPhoto: '',
            questionType:'',
            approve1: '',
            approve2: '',
            fileList: [],
        },

        rules: {
            subject: [
                { required: true, message: '请输入标题', trigger: 'blur' },
                { min: 0, max: 50, message: '长度在 0 到 50 个字符', trigger: 'blur' }
            ],
            questionType: [
                { required: true, message: '请选择问题类型', trigger: 'blur' },
            ],
            content: [
                { required: true, message: '请填写内容', trigger: 'blur' }
            ],
            approve1: [
                { required: true, message: '请选择审批人', trigger: 'blur' },
                // { validator: this.validateRange, trigger: 'change' },
                // { min: 0, max: 50,  trigger: 'blur' }
            ],
            approve2: [
                { required: true, message: '请选择审批人', trigger: 'blur' },
                // { required: true,validator: (rule, value, callback) => {
                //         if (value!=='' && value === vm.process.approve1) {
                //             callback(new Error('请选择不同的审批人'));
                //         } else {
                //             callback();
                //         }
                //     }, trigger: 'change' },
            ],
            etime: [
                { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
            ],
            date2: [
                { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
            ],
            type: [
                { type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
            ],
            resource: [
                { required: true, message: '请选择活动资源', trigger: 'change' }
            ],
            desc: [
                { required: true, message: '请填写活动形式', trigger: 'blur' }
            ],

        },



    },


    methods: {

        // validateRange  (rule, value,callback) {
        //     if (this.process.approve1 === this.process.approve2) {
        //         console.log("23232")
        //         callback(new Error('请选择不同的审批人'));
        //
        //     } else {
        //         console.log("11111")
        //         callback();
        //     }
        // },
        handleChange(file, fileList) {
            // console.log(this.process.fileList.at(0))
            this.process.fileList = fileList.slice(-3);
        },

        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
            this.process.questionPhoto = file.name;
        },
        beforeAvatarUpload(file) {


            // const isJPG = file.type === 'image/*';
            const isLt2M = file.size / 1024 / 1024 < 100;

            // if (!isJPG) {
            //     this.$message.error('上传的文件只能是图片格式!');
            // }
            if (!isLt2M) {
                this.$message.error('上传文件大小不能超过 100MB!');
            }
            // return isJPG && isLt2M;
            return isLt2M
        },

        //创建工单
        submitProcess(process) {
            this.isClicked = true; // 点击后设置按钮为不可点击
            // 执行异步操作，例如发送请求

            this.$refs[process].validate((valid) => {
                if (valid) {
                    console.log(this.process.fileList)
                    axios({
                        method: 'post',
                        url: '/add_process',
                        headers:{
                            'Context-Type': 'application/json' // 1
                        },
                        data: this.process// 2
                    }).then((res) => {
                        if(vm.process.approve1){
                            alert("服务单创建成功，待审核")
                        }
                        else {
                            alert('服务单创建成功')
                        }
                        // this.isClicked = false;
                        top.location.href = "index.html"
                    });
                } else {
                    this.isClicked = false;
                    return false;
                }
            });


        },

        resetForm(process) {
            this.$refs[process].resetFields();
        },
    },
    created: function () {

        axios.get(
            "/getAllName",{
            }
        ).then(function (res) {
            vm.user=res.data
        }).catch(e => {
            ajaxError(vm, e);
        });

        //获取自己的姓名
        axios.get(
            "/getMy",{
            }
        ).then(function (res) {
            vm.process.proposer=res.data
        }).catch(e => {
            ajaxError(vm, e);
        });


        axios.get(
            "/getQuestion",{
            }
        ).then(function (res) {
            vm.question=res.data
        }).catch(e => {
            ajaxError(vm, e);
        });
        this.process.questionType = 1


    },
});



